package com.cxl.demo.interceptor;

import com.alibaba.fastjson.JSON;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.service.UserService;
import com.cxl.demo.util.JWTUtil;
import com.cxl.demo.vo.ReturnData;
import com.cxl.demo.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    ReturnData returnData;
    @Autowired
    UserMapper userMapper;
    @Autowired
    User user;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=utf-8");
        System.out.println(request.getServletPath());
        String path="";
            if(path.equals("/user/login")||path.equals("/user/register")||path.equals("/file/UploadImage")){
                return true;
            }




        if(request.getHeader("token")==null){
            returnData.setData(null);
            returnData.setStatus(false);
            returnData.setMsg("token空,请求不合法");
            JSON json1 = (JSON) JSON.toJSON(returnData);
            System.out.println("1");
            writer = response.getWriter();
            writer.print(json1);
            if (writer != null)
                writer.close();
            return false;

        }else if(!new JWTUtil().TokenTime((String) request.getHeader("token"))){
            returnData.setData(null);
            returnData.setStatus(false);
            returnData.setMsg("token过期重新登录");
            JSON json1 = (JSON) JSON.toJSON(returnData);
            writer = response.getWriter();
            writer.print(json1);
            System.out.println("2");
            if (writer != null)
                writer.close();
            return false;
        }else if(userMapper.findUserByUserId(new JWTUtil().getUserId(request.getHeader("token")))==null){
            returnData.setData(null);
            returnData.setStatus(false);
            returnData.setMsg("token不合法");
            JSON json1 = (JSON) JSON.toJSON(returnData);
            System.out.println("3");
            writer = response.getWriter();
            writer.print(json1);
            if (writer != null)
                writer.close();
            return false;
        }
        else{
            System.out.println("4");
            user.setUserId(new JWTUtil().getUserId(request.getHeader("token")));
            return true;
        }



    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
